package com.zttek.photosShareProject.modules.web.manager;

import com.baomidou.mybatisplus.plugins.Page;
import com.google.code.kaptcha.Constants;
import com.zttek.photosShareProject.common.base.MyBaseController;
import com.zttek.photosShareProject.common.myException.DBException;
import com.zttek.photosShareProject.common.utils.PasswordUtil;
import com.zttek.photosShareProject.modules.model.Admin;
import com.zttek.photosShareProject.modules.model.User;
import com.zttek.photosShareProject.modules.service.login.IUserService;
import com.zttek.photosShareProject.modules.service.manager.IAdminService;
import com.zttek.photosShareProject.modules.service.share.IInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by 13296 on 2017/8/12.
 */
@Controller
public class AdminController extends MyBaseController{
    @Autowired
    private IAdminService adminService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IInfoService infoService;


    /**
     * 管理员登录GET
     * @return
     */
    @RequestMapping(value = {"adminLogin"},method = RequestMethod.GET)
    public String adminLoginView(){
        return null;
    }

    /**
     * 管理员登录POST
     * @param password
     * @param account
     * @param verifyCode
     * @return
     */
    @RequestMapping(value = {"adminLogin"},method = RequestMethod.POST)
    public String adminLogin(String password, String account, String verifyCode, Model model,HttpServletRequest request){

        String code = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);        //获取生成的验证码
        System.out.println(verifyCode + "," + code);
        Admin admin =null;
        try {
            /*验证码验证*/
            if(!verifyCode.equals(code)) {
                throw new DBException("验证码错误！");
            }
            System.out.println("验证通过 ");

            /*用户验证 */
            admin = adminService.selectAdminByAccount(account);
            if(!PasswordUtil.verify(password,admin.getPassword())){
                throw new DBException("密码错误！");
            }
        } catch (Exception e) {
            model.addAttribute("adminLoginMessage",e.getMessage());
            e.printStackTrace();
            return null;
        }
        HttpSession session = request.getSession();
        session.setAttribute("currentAdmin",admin);
        return "redirect:admin/adminIndex";
    }

    /**
     * 管理员退出登录
     * @param request
     * @return
     */
    @RequestMapping(value = "admin/logout",method = RequestMethod.GET)
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.invalidate();
        return "redirect:/adminLogin";
    }

    @RequestMapping(value = "admin/adminIndex",method = RequestMethod.GET)
    public String adminIndexView(String addAdminMessage,Model model,HttpServletRequest request){
        if(request.getSession().getAttribute("currentAdmin")==null){
            return "redirect:/adminLogin";
        }
        model.addAttribute("addAdminMessage",addAdminMessage);
        return null;
    }

    @RequestMapping(value = "admin/adminIndex",method = RequestMethod.POST)
    public String adminIndex(Model model){
        return null;
    }

    @RequestMapping(value = "admin/addAdmin",method = RequestMethod.GET)
    public String addAdminView(Model model){
        return null;
    }

    @RequestMapping(value = "admin/addAdmin",method = RequestMethod.POST)
    public String addAdmin(String account,String password,String mobilephone,String email,Model model,HttpServletRequest request){
        if(request.getSession().getAttribute("currentAdmin")==null){
            return "redirect:/adminLogin";
        }
        Admin admin = new Admin();
        Date date = new Date();
        admin.setCreateTime(date);
        admin.setAccount(account);
        String passwordt = password;
        password = PasswordUtil.generate(password);
        admin.setPassword(password);
//        admin.setMobilephone(mobilephone);
        admin.setEmail(email);
        try {
            List<Admin> list = adminService.selectAllAdmin();
            for(Admin a:list){
                if(a.getAccount().equals(account)){
                    throw new DBException("该账号已存在");
                }
            }
            adminService.insertAdmin(admin);
        } catch (Exception e) {
            model.addAttribute("addAdminMessage",e.getMessage());
            model.addAttribute("password",passwordt );
            model.addAttribute("mobilephone",mobilephone);
            model.addAttribute("email",email);
            e.printStackTrace();
            return null;
        }
        System.out.println("添加管理员成功！");
        model.addAttribute("addAdminMessage","success");
        return "redirect:adminIndex";
    }

    @RequestMapping(value = "admin/manageUser",method = RequestMethod.GET)
    public String manageUserView(int pn,Model model,HttpServletRequest request){
        if(request.getSession().getAttribute("currentAdmin")==null){
            return "redirect:/adminLogin";
        }
        List<User> list = null;
        Page<User> page = new Page<>(pn,10);
        try {
            page = userService.selectUserPage(page);
            list = page.getRecords();
            model.addAttribute("pages",page.getPages());
            model.addAttribute("userList",list);
            model.addAttribute("pn",pn);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 编辑管理员信息
     * @param model
     * @return
     */
    @RequestMapping(value = "admin/editAdmin",method = RequestMethod.GET)
    public String editAdminView(Model model){
        return null;
    }

    @RequestMapping(value = "admin/editAdmin",method = RequestMethod.POST)
    public String editAdmin(String oldPassword,String newPasssword,String email,Model model,HttpServletRequest request){
        if(request.getSession().getAttribute("currentAdmin")==null){
            return "redirect:/adminLogin";
        }

        return null;
    }

    /**
     * 改变分享状态
     * @param id
     * @param pn
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "admin/manageUser",method = RequestMethod.POST)
    public User manageUser(Long id,int pn,Model model){
        User user = null;
        try {
            user = userService.selectUserById(id);
            user.setIsShare(user.getIsShare()==0?1:0);
            System.out.println("user-isShare:"+user.getIsShare());
            userService.updateUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    @RequestMapping(value = "admin/searchUser",method = RequestMethod.GET)
    public String searchUserView(int pn,String searchUserValue,Model model,HttpServletRequest request){
        if(request.getSession().getAttribute("currentAdmin")==null){
            return "redirect:/adminLogin";
        }
        List<User> list = null;
        Page<User> page = new Page<>(pn,10);
        try {
            page = userService.selectLike(page,searchUserValue);
            list = page.getRecords();
            model.addAttribute("pages",page.getPages());
            model.addAttribute("userList",list);
            model.addAttribute("pn",pn);
            model.addAttribute("searchUserValue",searchUserValue);
            if (searchUserValue.trim().length()==0&&searchUserValue.trim().equals("")){
                model.addAttribute("emptyValue",true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "admin/searchUser",method = RequestMethod.POST)
    public String searchUser(int pn,String searchUserValue,Model model,HttpServletRequest request){
        if(request.getSession().getAttribute("currentAdmin")==null){
            return "redirect:/adminLogin";
        }
        searchUserView(pn,searchUserValue,model,request);
        return null;
    }


}
